package com.evaluationSystem.util;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Data
@EqualsAndHashCode(callSuper = true)
public class ExcelListener extends AnalysisEventListener<Map<Integer, String>> {
    private static final int BATCH_COUNT = 10;
    //Excel数据缓存结构
    private List<Map<Integer, Map<Integer, String>>> list = new ArrayList<>();
    //Excel表头（列名）数据缓存结构
    private Map<Integer, String> headTitleMap = new HashMap<>();

    private int total = 0;

    public ExcelListener() {

    }


    /**
     * 每条数据都调用
     **/
    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext analysisContext) {
        Map<Integer, Map<Integer, String>> map = new HashMap<>();
        map.put(analysisContext.readRowHolder().getRowIndex(), data);
        list.add(map);
    }

    /**
     * 所有数据解析完成调用以下方法
     **/
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("数据解析完成");
    }

    /**
     * 解析表头数据
     **/
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        //Excel表头（列名）数据缓存结构
        log.info("表头数据：{}", JSONObject.toJSONString(headMap));
        headTitleMap = headMap;
    }

}
